package slowka;

import java.awt.Color;
import java.awt.event.*;
import java.util.*;

/**
 *
 * @author ssobczak
 */
public class Test extends javax.swing.JFrame {
    private ResourceBundle resourceBundle;
    /** Creates new Test form */
    public Test(List<Suite> s, QuestionAskingMode m, ActionListener c, ResourceBundle rb) {
        callback = c;
        resourceBundle = rb;
        
        questions = new QuestionsSuite(s, m, rb);

        initComponents();

        tt = new TimerThread(TimerLabel, new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                CheckButtonActionPerformed(e);
            }
        });

        askQuestion();       
    }

    /**
     * Callback to caller before disposing
     */
    @Override
    public void dispose() {
        callback.actionPerformed(new ActionEvent(this, 1, "appear"));
        super.dispose();
    }

    @SuppressWarnings("unchecked")
    private void initComponents() {//GEN-BEGIN:initComponents

        jLabel1 = new javax.swing.JLabel();
        CategoryNameLabel = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        WordLabel = new javax.swing.JLabel();
        InputTextField = new javax.swing.JTextField();
        CheckButton = new javax.swing.JButton();
        StopButton = new javax.swing.JButton();
        StatusLabel = new javax.swing.JLabel();
        OkButton = new javax.swing.JButton();
        ProgressBar = new javax.swing.JProgressBar();
        jLabel3 = new javax.swing.JLabel();
        TimerLabel = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Test");

        jLabel1.setText(resourceBundle.getString("QUESTION_CATEGORY") + ":");

        CategoryNameLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        CategoryNameLabel.setText(resourceBundle.getString("CATEGORY")); // NOI18N

        jLabel2.setText(resourceBundle.getString("TRANSLATE") + ":");

        WordLabel.setFont(new java.awt.Font("Tahoma", 1, 12));
        WordLabel.setText(resourceBundle.getString("WORD")); // NOI18N

        InputTextField.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        InputTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                InputTextFieldKeyTyped(evt);
            }
        });

        CheckButton.setText(resourceBundle.getString("CHECK")); // NOI18N
        CheckButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CheckButtonActionPerformed(evt);
            }
        });

        StopButton.setText(resourceBundle.getString("STOP_TEST")); // NOI18N
        StopButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StopButtonActionPerformed(evt);
            }
        });

        StatusLabel.setFont(new java.awt.Font("Tahoma", 1, 14));
        StatusLabel.setText(resourceBundle.getString("LET'S_BEGIN") + "!");

        OkButton.setText(resourceBundle.getString("OK"));
        OkButton.setVisible(false);
        OkButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OkButtonActionPerformed(evt);
            }
        });

        ProgressBar.setBackground(new java.awt.Color(255, 0, 0));
        ProgressBar.setForeground(new java.awt.Color(51, 255, 51));

        jLabel3.setText(resourceBundle.getString("TIME_LEFT"));

        TimerLabel.setFont(new java.awt.Font("Tahoma", 1, 11));
        TimerLabel.setText("0");

        jButton1.setText(resourceBundle.getString("PAUSE"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(StatusLabel))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(WordLabel))))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(InputTextField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(CheckButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(OkButton, javax.swing.GroupLayout.PREFERRED_SIZE, 75, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(StopButton, javax.swing.GroupLayout.PREFERRED_SIZE, 95, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(ProgressBar, javax.swing.GroupLayout.DEFAULT_SIZE, 265, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel3)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(CategoryNameLabel)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(TimerLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(TimerLabel)
                    .addComponent(jButton1))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(CategoryNameLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(StatusLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(WordLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(InputTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(CheckButton)
                    .addComponent(OkButton)
                    .addComponent(StopButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(ProgressBar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }//GEN-END:initComponents

    /** Closes the window */
    private void StopButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StopButtonActionPerformed
        dispose();
    }//GEN-LAST:event_StopButtonActionPerformed

    /** Checks given answer */
    private void CheckButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CheckButtonActionPerformed
        tt.setNextAction(TimerThread.WhatToDo.finish);
        timer.interrupt();
        
        if(questions.checkAnswer(InputTextField.getText())) {
            StatusLabel.setText(resourceBundle.getString("YOU'RE_RIGHT"));
            StatusLabel.setForeground(Color.green);
            InputTextField.setText("");

            askQuestion();
        }
        else {
            StatusLabel.setText(resourceBundle.getString("NO!") + "\n" + questions.getCurrentAnswer());
            StatusLabel.setForeground(Color.red);
            OkButton.setEnabled(false);
            showOk();
            
            OkButton.setEnabled(true);
        }
        refreshProgress();
    }//GEN-LAST:event_CheckButtonActionPerformed

    /** Prepares window for next question and asks it */
    private void OkButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OkButtonActionPerformed
        OkButton.setVisible(false);
        CheckButton.setVisible(true);
        StopButton.setVisible(true);
        jButton1.setVisible(true);
        InputTextField.setText("");
        InputTextField.setEditable(true);
        StatusLabel.setText("");
        askQuestion();
    }//GEN-LAST:event_OkButtonActionPerformed

    /** If enter typed, checks the answer */
    private void InputTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_InputTextFieldKeyTyped
        if(evt.getKeyChar() == KeyEvent.VK_ENTER) {
            if(CheckButton.isVisible())
                CheckButtonActionPerformed(null);
            else if(OkButton.isVisible())
                OkButtonActionPerformed(null);
        }
    }//GEN-LAST:event_InputTextFieldKeyTyped

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(isRunning) {
            WordLabel.setVisible(false);
            InputTextField.setVisible(false);
            CheckButton.setVisible(false);

            jButton1.setText(resourceBundle.getString("RESUME"));
            tt.setNextAction(TimerThread.WhatToDo.wait);
            timer.interrupt();
            isRunning = false;
        }
        else {
            WordLabel.setVisible(true);
            InputTextField.setVisible(true);
            CheckButton.setVisible(true);
            
            jButton1.setText(resourceBundle.getString("PAUSE"));
            timer.interrupt();
            isRunning = true;
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel CategoryNameLabel;
    private javax.swing.JButton CheckButton;
    private javax.swing.JTextField InputTextField;
    private javax.swing.JButton OkButton;
    private javax.swing.JProgressBar ProgressBar;
    private javax.swing.JLabel StatusLabel;
    private javax.swing.JButton StopButton;
    private javax.swing.JLabel TimerLabel;
    private javax.swing.JLabel WordLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables

    QuestionsSuite questions;
    ActionListener callback;
    TimerThread tt;
    Thread timer;
    boolean isRunning;

    /** Asks next question or finishes the test */
    private void askQuestion() {
        if(questions.drawNextQuestion()) {
            CategoryNameLabel.setText(resourceBundle.getString(questions.currentQuestion().getCategory()));
            WordLabel.setText(questions.currentQuestion().getQuestion());

            restartTimer();
        }
        else {
            StatusLabel.setText(StatusLabel.getText() + resourceBundle.getString("TEST_FINISHED!"));
            new StatsWindow(questions, resourceBundle).setVisible(true);
            StatusLabel.setForeground(Color.green);
            CheckButton.setEnabled(false);
            StopButton.setText(resourceBundle.getString("FINISH!"));
        }
    }

    private void showOk() {
        OkButton.setVisible(true);
        CheckButton.setVisible(false);
        StopButton.setVisible(false);
        InputTextField.setEditable(false);
        jButton1.setVisible(false);
    }

    private void refreshProgress() {
        ProgressBar.setValue((questions.getTotalCount() - questions.getRemaningCount()) * 100 / questions.getTotalCount());
    }

    private void restartTimer() {
        tt.restart(10);
        timer = new Thread(tt);
        timer.start();
        isRunning = true;
    }
}
